//
//  BBHistoryViewController.m
//  BlockBrowser
//
//  Created by apple on 2018/4/8.
//  Copyright © 2018年 stoneobs@icloud.com. All rights reserved.
//

#import "BBHistoryViewController.h"
#import "BBScanHistoryModel.h"
#import "BBScanHistoryTableViewCell.h"
#import "BBInviteViewController.h"
#import "UITableView+STNoresult.h"
@interface BBHistoryViewController ()
@property(nonatomic, strong) STSegementButton     *segementButton;
@property(nonatomic, strong) NSArray     *dataSouce;
@end

@implementation BBHistoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configSubView];
    // Do any additional setup after loading the view.
}
#pragma mark --SubView
- (void)configSubView{
    __weak typeof(self) weakSelf = self;
    self.segementButton = [[STSegementButton alloc] initWithFrame:CGRectMake(0, 0, 100, 44) andTitle:@[@"收藏",@"历史"] handle:^(STSegementButton *sender) {
        [weakSelf.tableView reloadData];
    }];
    self.segementButton.backgroundColor = [BBThemeColorManger manger].themeColor;
    
    self.segementButton.lineSelectedColor = [UIColor whiteColor];
    self.segementButton.butTitleSelectedColor = [UIColor whiteColor];
    self.segementButton.butTitleColor = SecendTextColor;
    self.segementButton.butbackGroundColor = [UIColor clearColor];
    self.segementButton.lineColor = [UIColor clearColor];
    self.segementButton.cureentIndex = 0;
    self.segementButton.autoMoveWithClic = YES;
    self.navigationItem.titleView = self.segementButton;
    
    self.tableView.st_noreslutView = [[STNoresultView alloc] initWithFrame:CGRectMake(0, 150, UIScreenWidth, 400) title:@"暂无数据" buttonTitle:@"" buttonHandle:^(NSString *titleString) {
        
    }];

}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1    ;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.segementButton.cureentIndex == 0) {
        return [BBHistoryManger manger].collectionArray.count;
    }
    return [BBHistoryManger manger].scanArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [BBScanHistoryTableViewCell cellHeight];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    BBScanHistoryTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[BBScanHistoryTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
        
    }
    cell.accessoryType = UITableViewCellAccessoryNone;
    
    BBScanHistoryModel * model ;
    if (self.segementButton.cureentIndex == 1) {
        model = [BBHistoryManger manger].scanArray[indexPath.row];
    }else{
        model = [BBHistoryManger manger].collectionArray[indexPath.row];
    }
    
    cell.iconImageView.image = model.iconImage;
    cell.titlable.text = model.title;
    cell.subTitleLable.text = model.url;
    if (BBThemeColorManger.isBlack) {
        cell.backgroundColor = [BBThemeColorManger manger].backGroundColor;
    }
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    BBScanHistoryModel * model ;
    if (self.segementButton.cureentIndex == 1) {
        model = [BBHistoryManger manger].scanArray[indexPath.row];
    }else{
        model = [BBHistoryManger manger].collectionArray[indexPath.row];
    }
    STWebViewController * webvc =  [[STWebViewController alloc] initWithUrl:model.url];
    webvc.forceTitle = model.title;
    webvc.shouldNotRecord = YES;
    [self.navigationController pushViewController:webvc animated:YES];
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}

@end
